package model

import (
	"gorm.io/gorm"
)

type Passage struct {
	PassageID      int    `gorm:"primaryKey; autoIncrement"`
	AuthorID       int    `gorm:"not null"`
	PassageTitle   string `gorm:"not null"`
	PassagePic     string `gorm:"not null"`
	PassageContent string `gorm:"not null"`
	CommentNum     int    `gorm:"not null"`
	LikeNum        int    `gorm:"not null"`
	PassageTime    string
	PassageType    string `gorm:"not null"` // 0: 日常帖；1: 领养帖
	PassageArea    string
	User           *User          `gorm:"foreignKey:AuthorID"`
	DeletedAt      gorm.DeletedAt `gorm:"index"`
}

type UserList struct {
	PassageArea  *string `json:"PassageArea,omitempty"`
	PassageID    int64   `json:"PassageID"`
	PassagePic   string  `json:"PassagePic"`
	PassageTitle string  `json:"PassageTitle"`
	PassageType  string  `json:"PassageType"`
	// IfLiked      bool    `json:"IfLiked"`
}

type SquareList struct {
	AuthorID     int64   `json:"AuthorID"`
	PassageArea  *string `json:"PassageArea,omitempty"`
	PassageID    int64   `json:"PassageID"`
	PassagePic   string  `json:"PassagePic"`
	PassageType  string  `json:"PassageType"`
	PassageTitle string  `json:"PassageTitle"`
}

func GetAuthorID(passageID int) (int, error) {
	var err error
	var authorID int
	err = DB.Model(Passage{}).Select("AuthorID").Where("PassageID = ?", passageID).Find(&authorID).Error
	if err != nil {
		return authorID, err
	}
	return authorID, nil
}

type PassagePic struct {
	PassagePic string `json:"PassagePic"`
}

func GetTitleByID(id int) (string, error) {
	var err error
	var title string

	err = DB.Model(Passage{}).Select("PassageTitle").Where("PassageID = ?", id).Find(&title).Error
	if err != nil {
		return title, err
	}
	return title, nil
}

func GetPassageTypeByID(id int) (string, error) {
	var passageType string
	err := DB.Model(Passage{}).Select("PassageType").Where("PassageID = ?", id).Find(&passageType).Error
	if err != nil {
		return passageType, err
	}
	return passageType, nil
}

func GetUserList(authorID int) ([]UserList, error) {
	var userList []UserList
	err := DB.Model(&Passage{}).Where("AuthorID=?", authorID).Find(&userList).Error
	if err != nil {
		return userList, err
	}
	return userList, nil
}

func GetFocusList(authorID int) ([]SquareList, error) {
	var focusList []SquareList
	err := DB.Model(&Passage{}).Where("AuthorID=?", authorID).Find(&focusList).Error
	if err != nil {
		return focusList, err
	}
	return focusList, nil
}

func GetSquareList() ([]SquareList, error) {
	var squareList []SquareList
	err := DB.Model(&Passage{}).Find(&squareList).Error
	if err != nil {
		return squareList, err
	}
	return squareList, nil
}

func GetAdoptList() ([]SquareList, error) {
	var adoptList []SquareList
	err := DB.Model(&Passage{}).Where("PassageType=?", "1").Find(&adoptList).Error
	if err != nil {
		return adoptList, err
	}
	return adoptList, nil
}

func SearchGlobalList(search string) ([]SquareList, []SquareList, error) {
	var globalList []SquareList
	var adoptList []SquareList

	searchMessage := "%" + search + "%"
	err := DB.Model(&Passage{}).Where("PassageTitle LIKE ?", searchMessage).Or("PassageContent LIKE ?", searchMessage).Find(&globalList).Error
	if err != nil {
		return globalList, adoptList, err
	}
	err = DB.Model(&Passage{}).Where("PassageTitle LIKE ? AND PassageType = ?", searchMessage, "1").Or("PassageContent LIKE ? AND PassageType = ?", searchMessage, "1").Find(&adoptList).Error
	if err != nil {
		return globalList, adoptList, err
	}
	return globalList, adoptList, nil
}

func SearchUserList(search string, id int) ([]SquareList, error) {
	var userList []SquareList

	searchMessage := "%" + search + "%"
	err := DB.Model(&Passage{}).Where("PassageTitle LIKE ? AND AuthorID = ?", searchMessage, id).Or("PassageContent LIKE ? AND AuthorID = ?", searchMessage, id).Find(&userList).Error
	if err != nil {
		return userList, err
	}
	return userList, nil
}

func UploadPassage(upload Passage) (int, error) {
	err := DB.Model(&Passage{}).Create(&upload).Error
	if err != nil {
		return -1, err // 插入失败时返回错误
	}
	return upload.PassageID, nil
}

func UpdateArea(pid int, path string) error {
	err := DB.Model(&Passage{}).Where("PassageID = ?", pid).Update("PassagePic", path).Error
	return err
}

func DeletePassage(pid int) error {
	err := DB.Where("PassageID = ?", pid).Delete(&Passage{}).Error
	return err
}

func GetPassageContent(pid int) (Passage, error) {
	var passage Passage
	err := DB.Model(&Passage{}).Where("PassageID = ?", pid).Take(&passage).Error
	return passage, err
}

func GetPassagePic(pid int) (PassagePic, error) {
	var passagePic PassagePic
	err := DB.Model(&Passage{}).Where("PassageID = ?", pid).Take(&passagePic).Error
	return passagePic, err
}

func UpdatePassageLikeNum(num int, passageID int) error {
	err := DB.Model(&Passage{}).Where("PassageID = ?", passageID).Update("LikeNum", gorm.Expr("LikeNum + ?", num)).Error
	return err
}
